import { _decorator, Component, Node, Enum, log } from 'cc';
import { EDITOR } from 'cc/env';
import { KeyActionHandler } from './KeyActionHandler';
import { KeyAction, KeyActionInfo, KeyActionType } from './KeyActionDefine';
import { SuperButton } from '../base/SuperButton';
import { InputKeyCode } from '../../core/InputConfig';
import GameInputMgr from '../../core/GameInputMgr';
const { ccclass, property, requireComponent } = _decorator;

@ccclass('SuperButtonHandler')
@requireComponent([SuperButton])
export class SuperButtonHandler extends KeyActionHandler {

    @property(SuperButton)
    sb: SuperButton = null;


    start() {
        this.initHandlers();
        super.start();
    }
    reset() {
        if (!this.sb) {
            return;
        }
        let data: KeyAction = KeyActionInfo[KeyActionType[this.actionType]]
        if (!data) {
            log(' 数据不存在 ', this.actionType, KeyActionType[this.actionType])
        } else {
            let code: InputKeyCode = GameInputMgr.instance().converter.getKeyValueByKeyName(data.keyCodes[0]);

            if (code) {
                // this.sb.keyCode = code
                if (!EDITOR) {
                    this.sb.setKeyCode(code)
                    if (this.sb.keyTipSprite)
                        this.actionTip = this.sb.keyTipSprite;
                    if (this.sb.keyTipLabel)
                        this.actionName = this.sb.keyTipLabel;
                    this.setKeyTipSprite()
                    this.setBtnName()
                    if (this.model) {
                        // this.sb.setKeyTipLabel(this.model.name)
                        this.sb.kcHandler.soundEventId = this.model.soundEventId;
                    }
                }
            }

        }
        this.sb.kcHandler.isContinuous = this.isContinuous;
        this.sb.kcHandler.continuousDuration = this.continuousDuration;
        this.sb.kcHandler.isBelongKeyBoard = this.isBelongKeyBoard;
    }
    initHandlers() {
        if (EDITOR) {
            this.removeAllKeyCodeHandler();
            return;
        }
        // Debug.log(' initHandlers ', this.actionType)
        if (this.kcHandlers.length > 0 || this.actionType == KeyActionType.none) {
            log('已经初始化过了 ')
            return;//已经初始化过了
        }
        if (!this.sb) {
            this.sb = this.node.getComponent(SuperButton)
        }
        if (this.sb) {
            // if (this.actionType != KeyActionType.none) {
            this.reset()

            this.kcHandlers.push(this.sb.kcHandler)
        }
    }

    update(deltaTime: number) {

    }
}

